﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiYiTong.Rocket.EFDBFirst;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Model.Model;

namespace YiYiTong.Rocket.Business
{
    public class NosignBiz
    {
        private Dictionary<string, NosignModel> nosignModelDic = new Dictionary<string, NosignModel>();

        #region 单例模式
        private void init()
        {
            nosignModelDic = new Dictionary<string, NosignModel>();
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var we = dbcontext.ADZ_ApiNosign.Where(t => t.nosign == 0).ToList();
            if (we.Any())
            {
                we.ForEach(t =>
                {
                    NosignModel model = new NosignModel();
                    Utils.Mapper(model, t, new List<string>());
                    nosignModelDic.Add(t.service.ToLower(), model);
                });
            }
        }

        private static NosignBiz _instance;
        private static readonly object _syncObject = new object();

        public static NosignBiz Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new NosignBiz();
                            _instance.init();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion 单例模式

        /// <summary>
        /// 
        /// </summary>
        /// <param name="apiservice"></param>
        /// <returns>
        /// 返回true表示不需要签名校验
        /// </returns>
        public bool CheckNoSign(string apiservice)
        {
            if (nosignModelDic == null)
            {
                init();
            }
            apiservice = apiservice.ToLower();
            NosignModel model;
            if(nosignModelDic.TryGetValue(apiservice, out model))
            {
                if(model.nosign == 0)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
